package controller;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLIntegrityConstraintViolationException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Address;
import model.DatabaseException;
import model.Registration;
import model.User;

/**
 * Servlet implementation class Register
 */
@WebServlet("/Register")
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
//		System.out.println("RegisterServlet");
		  
		String username = request.getParameter("login_name");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String gender =  request.getParameter("gender");
		String streetLine1 = request.getParameter("address1");
		String streetLine2 = request.getParameter("address2");
		String city =  request.getParameter("city");
		String postalCode =  request.getParameter("postCode");
		String region = request.getParameter("region");
		String country = request.getParameter("country");
		String bulletin = request.getParameter("bulletin");
		
		HttpSession session = request.getSession();
		Object requestedAccountType = session.getAttribute("requestedAccountType");
		String accountType = (requestedAccountType == null) ? "U" : (String)requestedAccountType;
		
//		System.out.println("the username is " +username);
//		System.out.println("the password is "+password);
//		System.out.println("the name is " +name);
//		System.out.println("the email is "+email);
//		System.out.println("the phone is "+phoneNumber);
//		System.out.println("the city is "+city);
//		System.out.println("the address1 is "+address1);
//		System.out.println("the address2 is "+address2);
//		System.out.println("the region is "+region);
//		System.out.println("the country is: "+country);
//		System.out.println("the gender is "+gender);
//		System.out.println("the bulletin is "+ bulletin);
//		System.out.println("the postcode is "+ postCode);
//		System.out.println("the account type is "+accountType);
		
		User user = new User(-1, username, password, name, email, phone, gender, bulletin, accountType);
		Address address = new Address(-1, country, region, city, streetLine1, streetLine2, postalCode);
		PrintWriter out =response.getWriter();
		try {
			Registration.register(user,address);
			if(accountType.equals("U")){
				session.setAttribute("user", user);
			}
			out.print("index.html");
		} catch (DatabaseException e) {
			Throwable cause = e.getCause();
			if(cause instanceof  SQLIntegrityConstraintViolationException){
				//Last moment coincidence of user names
				out.print("coincidence");
			}else{
				//Database problem
				out.print("TechnicalDifficulties.html");
			}
			
		}
	}

}
